﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using LIB;
using System.Collections;
using System.Collections.Generic;


/// <summary>
/// Summary description for App_String
/// </summary>

namespace LIB
{
    public class App_String
    {
        public App_String()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        public static string OptimiseContent(string source)
        {
            try
            {
                string result;

                // Remove HTML Development formatting
                result = source.Replace("\r", " ").Replace("\n", "").Replace("\t", "").Replace("\n\r", "").Replace("\r\n", "").Replace("\"", "\"\""); // Replace line breaks with space because browsers inserts space
                result = result.Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "");
                result = Regex.Replace(result, @"( )*font([^>])*", "", RegexOptions.IgnoreCase);
                result = result.Replace("</font>", "");
                result = Regex.Replace(result, "( )*style=\\\"font([^>])*", "", RegexOptions.IgnoreCase);

                result = Regex.Replace(result, @"<p>", "", RegexOptions.IgnoreCase);
                result = result.Replace("</p>", "");

                result = result.Replace("<br>", "");

                result = Regex.Replace(result, @"<span>", "", RegexOptions.IgnoreCase);
                result = result.Replace("</span>", "");

                result = Regex.Replace(result, @"<div>", "", RegexOptions.IgnoreCase);
                result = result.Replace("</div>", "");

                return result;

            }
            catch
            {
                return source;
            }
        }
        public static string getHtmlWithFormated(string sHtml)
        {
            string sResult;
            sResult = sHtml.Replace("'", "\'");
            sResult = sResult.Replace(Convert.ToChar(10).ToString(), "");
            sResult = sResult.Replace(Convert.ToChar(13).ToString(), "");

            sResult = sResult.Replace(Convert.ToChar(147).ToString(), "\"");
            sResult = sResult.Replace(Convert.ToChar(148).ToString(), "\"");
            return sResult;
        }

        public static string getTitleKhongDau(string sTitle)
        {
            sTitle = sTitle.Trim();
            string _tmp = UnicodeUtility.UnicodeToKoDauForUrl(sTitle);

            _tmp = _tmp.Replace("(", "");
            _tmp = _tmp.Replace(")", "");
            _tmp = _tmp.Replace("&", "");
            _tmp = _tmp.Replace("!", "");
            _tmp = _tmp.Replace("@", "");
            _tmp = _tmp.Replace("#", "");
            _tmp = _tmp.Replace("$", "");
            _tmp = _tmp.Replace("%", "");
            _tmp = _tmp.Replace("^", "");
            _tmp = _tmp.Replace("*", "");
            _tmp = _tmp.Replace("+", "");
            _tmp = _tmp.Replace("=", "");
            _tmp = _tmp.Replace("{", "");
            _tmp = _tmp.Replace("}", "");
            _tmp = _tmp.Replace("[", "");
            _tmp = _tmp.Replace("]", "");
            _tmp = _tmp.Replace("\"", "");
            _tmp = _tmp.Replace("'", "");
            _tmp = _tmp.Replace("<", "");
            _tmp = _tmp.Replace(">", "");
            _tmp = _tmp.Replace(",", "");
            _tmp = _tmp.Replace(".", "");
            _tmp = _tmp.Replace("?", "");
            _tmp = _tmp.Replace("/", "-");
            _tmp = _tmp.Replace(":", "-");
            _tmp = _tmp.Replace(" ", "-");
          
            _tmp = Regex.Replace(_tmp, "-{2,}", "-", RegexOptions.Multiline);
            return _tmp;

        }
        public static List<T> MixList<T>(List<T> inputList)
        {
            try
            {
                var randomList = new List<T>();
                if (inputList.Count == 0)
                    return randomList;

                Random r = new Random();
                int randomIndex = 0;
                while (inputList.Count > 0)
                {
                    randomIndex = r.Next(0, inputList.Count); //Choose a random object in the list
                    randomList.Add(inputList[randomIndex]); //add it to the new, random list<>
                    inputList.RemoveAt(randomIndex); //remove to avoid duplicates
                }

                //clean up
                inputList.Clear();
                inputList = null;
                r = null;

                return randomList; //return the new random list
            }
            catch
            {
                return null;
            }
        }
    }
}

